package com.example.smart_city_0829.activity.waste_sorting;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.waste_sorting.HotKeyWord;
import com.example.smart_city_0829.entity.waste_sorting.PostIm;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.view.MaxSizeGv;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class SearchActivity extends BaseActivity {

    private Banner banner;
    private LinearLayout linearLayout;
    private EditText et;
    private Button secBt;
    private LinearLayout linearLayout2;
    private MaxSizeGv gv;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        banner = (Banner) findViewById(R.id.banner);
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        et = (EditText) findViewById(R.id.et);
        secBt = (Button) findViewById(R.id.sec_bt);
        linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
        gv = (MaxSizeGv) findViewById(R.id.gv);

    }

    @Override
    protected void initData() {
        api.getGarbagePoster().enqueue(new NetCallBack<PostIm>() {
            @Override
            public void onResponse(@NonNull Call<PostIm> call, @NonNull Response<PostIm> response) {
                if (response.body() == null) return;

                final List<PostIm.DataBean> data = response.body().getData();
                banner.setImages(data.stream()
                        .map(PostIm.DataBean::getImgUrl)
                        .collect(Collectors.toList()));
                banner.setBannerTitles(data.stream()
                        .map(PostIm.DataBean::getTitle)
                        .collect(Collectors.toList()));
                banner.start();
                banner.startAutoPlay();
            }
        });

        api.getGarbageHotKeyWord().enqueue(new NetCallBack<HotKeyWord>() {
            @Override
            public void onResponse(@NonNull Call<HotKeyWord> call, @NonNull Response<HotKeyWord> response) {
                if (response.body() == null) return;

                final HotKeyWord data = response.body();
                gv.setAdapter(new GvAdapter(data.getData(), SearchActivity.this));
            }
        });
    }


    @Override
    protected void initEvent() {
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.setImageLoader(imageLoader);

        secBt.setOnClickListener((v) -> {
            final Intent intent = new Intent(context, SearchResultActivity.class);
            intent.putExtra("name",et.getText().toString());
            startActivity(intent);
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "搜索";
    }

    static class GvAdapter extends CommonAdapter<HotKeyWord.DataBean, CommonAdapter.CommTextViewViewHolder> {

        public GvAdapter(List<HotKeyWord.DataBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(HotKeyWord.DataBean data, int position, CommTextViewViewHolder viewHolder, View convertView, Context context) {
            viewHolder.tv.setText(String.valueOf(data.getKeyword()));
        }

        @Override
        protected CommTextViewViewHolder getViewHolder() {
            return new CommTextViewViewHolder();
        }

        @Override
        protected void initView(CommTextViewViewHolder viewHolder, View convertView) {
            viewHolder.tv = convertView.findViewById(R.id.text);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.text;
        }
    }


}